//
//  ShengChengFaHuoDanListCell.m
//  FactorySale
//
//  Created by 李 on 2020/1/14.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "ShengChengFaHuoDanListCell.h"
#import "SYBigImage.h"
@implementation ShengChengFaHuoDanListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.faHuoNumTF.keyboardType = UIKeyboardTypeNumberPad;
    self.priceTextField.keyboardType = UIKeyboardTypeDecimalPad;

    self.faHuoNumTF.delegate = self;
    self.priceTextField.delegate = self;
    
    [self.faHuoNumTF addTarget:self action:@selector(faHuoNumChange) forControlEvents:UIControlEventEditingDidEnd];// UIControlEventEditingChanged
    [self.priceTextField addTarget:self action:@selector(priceChange) forControlEvents:UIControlEventEditingDidEnd];
    
}
- (void)faHuoNumChange {
    if (self.faHuoNumTF.text.length >=8) {
        self.faHuoNumTF.text = [self.faHuoNumTF.text substringToIndex:8];
    }
    if (self.faHuoNumTFEndHandle) {

           self.faHuoNumTFEndHandle(self.faHuoNumTF.text);
       }
}
- (void)priceChange {
    if (self.priceTextField.text.length >=8) {
        self.priceTextField.text = [self.priceTextField.text substringToIndex:8];
    }
    if (self.priceChangedEndHandle) {
    
           self.priceChangedEndHandle(self.priceTextField.text);
       }
}
-(void)setModel:(WeiFaHuoDetailPartsListModel *)model
{
    _model = model;
    if (_model) {
        
        [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.attributeLogo] placeholderImage:[UIImage imageNamed:@"logo"]];
        SYBigImage * bigI = [[SYBigImage alloc]init];
        [self.iconImageView addGestureRecognizer:bigI];
        self.iconImageView.userInteractionEnabled = YES;
        self.nameLab.text = [NSString stringWithFormat:@"%@",model.name];
        self.faHuoNumTF.text = [NSString stringWithFormat:@"%@",_model.count];
        NSString *sale = [NSString stringWithFormat:@"%.2f",[_model.price doubleValue]];
        self.priceTextField.text = [NSString stringWithFormat:@"%@",sale];
        self.unitLabel.text = [NSString stringWithFormat:@"%@",_model.unit];
    }
}
#pragma UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (textField == self.faHuoNumTF) {
        NSArray *attt = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
        if (![attt containsObject:string]  && ![string  isEqualToString:@""]) {
            ShowToastWithText(@"只能输入数字");
            return NO;
        }else{
            return YES;
        }
    }else{
        //新输入的
        if (string.length == 0) {
            return YES;
        }
        NSString *checkStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
        //正则表达式（只支持两位小数）
        NSString *regex = @"^\\-?([1-9]\\d*|0)(\\.\\d{0,2})?$";
        //判断新的文本内容是否符合要求
        return [self isValid:checkStr withRegex:regex];
    }
}
- (BOOL) isValid:(NSString*)checkStr withRegex:(NSString*)regex
{
    NSPredicate *predicte = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [predicte evaluateWithObject:checkStr];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
